package com.cnunicom.common.excel;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.merge.AbstractMergeStrategy;
import com.cnunicom.modules.tra.entity.RecordEntity;
import com.google.common.primitives.Longs;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: dingziyuan
 * @Date: 2020/3/12 10:58
 * @Description: 记录导出的合并策略
 */
public class RecordMergeStrategy extends AbstractMergeStrategy {

    private List<RecordEntity> recordList;
    private Sheet sheet;

    public RecordMergeStrategy(List<RecordEntity> recordList) {
        this.recordList = recordList;
    }

    /**
     * 第一列
     */
    private void mergeFirstColumn(Integer col) {/*
        long unitCount = this.recordList.parallelStream().filter(item -> "其他单位".equals(item.getStringType())).count();
        CellRangeAddress cellRangeAddress0 = new CellRangeAddress(1, Math.toIntExact(unitCount)
                , col, col);
        sheet.addMergedRegionUnsafe(cellRangeAddress0);

        long startRow = Math.subtractExact(recordList.size(), unitCount);
        CellRangeAddress cellRangeAddress1 = new CellRangeAddress(Math.toIntExact(startRow)
                , recordList.size(), col, col);
        sheet.addMergedRegionUnsafe(cellRangeAddress1);
    */}

    /**
     * 第二列
     * 因为要排序所以要蛇皮处理
     */
    private void mergeSedColumn(Integer col) {

        Map<String, Long> counts = this.recordList.stream()
                .collect(Collectors.groupingBy(RecordEntity::getSuperiorName, Collectors.counting()));

        List<String> keys = this.recordList.stream().map(RecordEntity::getSuperiorName)
                .distinct().collect(Collectors.toList());

        int rowCnt = 1;
        for (String key : keys) {
            Long len = counts.get(key);
            CellRangeAddress cellRangeAddress = new CellRangeAddress(rowCnt, rowCnt + Math.toIntExact(len) - 1
                    , col, col);
            sheet.addMergedRegionUnsafe(cellRangeAddress);
            rowCnt += Math.toIntExact(len);
        }
    }

    /**
     * 全部合并
     */
    private void mergeAllColumn(Integer col) {
        int rowCnt = 1;
        CellRangeAddress cellRangeAddress = new CellRangeAddress(rowCnt, this.recordList.size(), col, col);

        sheet.addMergedRegionUnsafe(cellRangeAddress);

    }

    @Override
    protected void merge(org.apache.poi.ss.usermodel.Sheet sheet, Cell cell, Head head, Integer integer) {/*
        this.sheet = sheet;
        if (cell.getRowIndex() == 1) {
            switch (cell.getColumnIndex()) {
                case 0:
                    mergeFirstColumn(0);
                    break;
                case 1:
                    mergeSedColumn(1);
                    break;
                case 6:
                    mergeAllColumn(6);
                    break;
                case 7:
                    mergeFirstColumn(7);
                    break;
                case 8:
                    mergeSedColumn(8);
                    break;
                default:
                    break;
            }


        }
    */}

}
